package homework.james.xiao.bean;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;

public class Waiter {
	private OrdersHistory ordersHistory;
	private Orders orders;
	private Menu menu;
	private PrintWriter ps;
	private ObjectOutputStream oos;
	private boolean autosave = false;

	public Waiter(PrintWriter ps) throws FileNotFoundException {
		menu = Menu.getInstance();
		orders = new Orders();
		ordersHistory = OrdersHistory.getInstance();
		this.ps = ps;
	}

	public Waiter(ObjectOutputStream oos) throws FileNotFoundException {
		menu = Menu.getInstance();
		orders = new Orders();
		ordersHistory = OrdersHistory.getInstance();
		this.oos = oos;
	}

	public Waiter() throws FileNotFoundException {
		menu = Menu.getInstance();
		orders = new Orders();
		ordersHistory = OrdersHistory.getInstance();
		this.ps = new PrintWriter(System.out, true);
	}

	/**
	 * 将当天的订单记录到历史记录中
	 * 
	 * @author James Xiao
	 * @throws IOException
	 * @datetime Sep 18, 20092:50:51 PM
	 * @param isVerbose: sent response message or not
	 */
	public void saveOrder(boolean isVerbose) throws IOException {
		String message = "";
		if (this.orders.getOrders().size() > 0) {
			this.ordersHistory.addOrders(orders);
			ordersHistory.sortHistory();
			ordersHistory.writeToXML();
			orders = new Orders();
			message = "your order was saved";
		} else
			message = "your oders is empty, please book first";
		if (isVerbose)
			oos.writeObject(message);
	}

	/**
	 * 获取菜单
	 * 
	 * @throws FileNotFoundException
	 * @author James Xiao
	 * @datetime Sep 18, 20092:51:20 PM
	 */
	public void getMenu() throws IOException {
		oos.writeObject(menu);
		oos.flush();
		oos.reset();
	}

	/**
	 * 为name订购序号为number的dish
	 * 
	 * @param number
	 * @param name
	 * @return
	 * @author James Xiao
	 * @throws IOException
	 * @datetime Sep 18, 20092:51:28 PM
	 */
	public String bookDish(String number, String name) throws IOException {
		User u = new User(name);
		int d = Integer.valueOf(number) - 1;
		if (d >= menu.getItems().size())
			return "there isn't any dish as requested";
		else {
			Dish dish = menu.getItems().get(d);
			orders.addOrder(new Order(u, dish));
			if (autosave) {
				saveOrder(false);
			}
			return "OK, " + dish.getName() + " was booked for " + name;
		}
	}

	/**
	 * 获取本次订单
	 * 
	 * @author James Xiao
	 * @datetime Sep 18, 20092:51:52 PM
	 */
	public void getOrders() throws IOException {
		System.out.println("orders on serverside : ");
		orders.print(true, true);
		oos.writeObject(orders);
		oos.flush();
		oos.reset();
	}

	/**
	 * 获取历史订单
	 * 
	 * @author James Xiao
	 * @datetime Sep 18, 20092:52:07 PM
	 */
	public void getOrdersHistory() throws IOException {
		oos.writeObject(ordersHistory);
		oos.flush();
		oos.reset();
	}

	/**
	 * 显示帮助信息
	 * 
	 * @author James Xiao
	 * @datetime Sep 18, 20092:52:16 PM
	 */
	public void printHelp() {
		ps.println("Usage:");
		ps.println("menu\t\t\t\t|get the menu");
		ps.println("book [number] for [username]\t|book dished for user");
		ps.println("history\t\t\t\t| browse orders history");
		ps.println("orders\t\t\t\t| browse current orders");
		ps.println("save\t\t\t\t| save current orders");
		ps.println("changeAutosave\t\t\t|reverse autosave state");
		ps.println("exit\t\t\t\t| exit the system");
	}

	/**
	 * return true if the client want to exit
	 * 
	 * @param command
	 * @return
	 * @throws IOException
	 * @author James Xiao
	 * @datetime Sep 23, 20094:33:51 PM
	 */
	public boolean handleWithSerialize(String command) throws IOException {

		if (command.equalsIgnoreCase("menu")) {
			getMenu();
		} else if (command.equals("history")) {
			getOrdersHistory();
		} else if (command.equals("orders")) {
			getOrders();
		} else if (command.equals("help")) {
			// printHelp();
		} else if (command.startsWith("book")) {
			// TODO 对错误输入的检测不够全面
			String message = "";
			if (command.split(" ").length != 4) {
				message = "Wrong args for book command";
			} else {
				message = bookDish(command.split(" ")[1], command.split(" ")[3]);
			}
			oos.writeObject(message);
		} else if (command.equals("changeAutosave"))
			reverseAutosave();
		else if (command.equals("exit")) {
			return true;
		} else if (command.equals("save")) {
			saveOrder(true);
		}
		return false;
	}
	// TODO 删除本次订单中的某些订购项
	// TODO 清空本次订单
	// TODO 用户退出时检测本次订单是否已经保存
	// TODO 对历史订单重新排序

	/**
	 * 
	 * @author James Xiao  
	 * @throws IOException 
	 * @datetime Sep 24, 200912:50:58 PM
	 */
	private void reverseAutosave() throws IOException {
		autosave = !autosave;
		String state = autosave?"on":"off";
		oos.writeObject("Current autosave sate is: " + state);	
	}

}
